package com.digital.welcome.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.digital.welcome.entity.User;
import com.digital.welcome.entity.UserPhoto;
import com.digital.welcome.mapper.UserPhotoMapper;
import com.digital.welcome.response.Result;
import com.digital.welcome.service.UserPhotoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class UserPhotoServiceImpl extends ServiceImpl<UserPhotoMapper, UserPhoto> implements UserPhotoService {
    @Autowired
    private UserPhotoMapper userPhotoMapper;

    @Override
    public String getUserPhoto(User user) {
        UserPhoto userPhoto = userPhotoMapper.getUserPhoto(user);
        if (userPhoto == null) {
            return JSON.toJSONString(new Result<>().setCode(200).setMessage("不存在信息"));
        }
        return JSON.toJSONString(new Result<>().setCode(200).setMessage("查询成功").setData(userPhotoMapper.getUserPhoto(user)));
    }

    @Override
    public String insertUserPhoto(UserPhoto userPhoto) {
        List<UserPhoto> userPhotoList = userPhotoMapper.selectAll();
        for (UserPhoto u : userPhotoList
        ) {
            if (userPhoto.getUserName().equals(u.getUserName())) {
                return JSON.toJSONString(new Result<>().setCode(200).setMessage("该用户已存在"));
            }
        }
        userPhotoMapper.insert(userPhoto);
        return JSON.toJSONString(new Result<>().setCode(200).setMessage("插入成功"));
    }
}
